/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */
import { describe, expect, test } from '@jest/globals';

import { CompileModeEnum } from '../../../src/enum/compile-mode-enum';
import { initByStageEmptyApp, initExecuteCommand, runHvigor } from '../common/common';
import { ModuleBuildProfileModifier } from '../common/modifier/module-build-profile-modifier';

describe('hot reload pre execute', () => {
  test.concurrent('stage esmodule app', async () => {
    await initByStageEmptyApp(async (dir) => {
      const entryBuildProfile = new ModuleBuildProfileModifier(dir, 'entry');
      entryBuildProfile.setCompileMode(CompileModeEnum.ES_MODULE)
        .outputSync();

      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        tasks: 'buildHotReloadResource'
      });
      const result = runHvigor(dir, commands);
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
    });
  });

  test.concurrent('stage js bundle app', async () => {
    await initByStageEmptyApp(async (dir) => {
      const entryBuildProfile = new ModuleBuildProfileModifier(dir, 'entry');
      entryBuildProfile.setCompileMode(CompileModeEnum.JS_BUNDLE)
        .outputSync();

      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        tasks: 'buildHotReloadResource'
      });
      try {
        runHvigor(dir, commands, { stderr: 'pipe' });
      } catch (e: any) {
        // 不支持jsbundle模式
        expect(e.stderr).toContain('Hot reload is only supported for esmodule mode in the stage model.');
      }
    });
  });

});
